<!--
  * 信息变更 弹窗
  * 
-->
<template>
  <a-modal v-model:visible="visible" :title="changeState?'变更状态':'采集阶段'" destroyOnClose>
    <a-form ref="formRef" :model="form" >
      <a-form-item label="项目名称" name="updateValue">
        <DictSelectType :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="changeState?DICT_TYPE_ENUM.STATE.value:DICT_TYPE_ENUM.PROJECT_COLLECT_STATE.value"  :height="400" v-model:value="form.updateValue" />
      </a-form-item>
      <a-form-item label="变更日期" name="updateDate" v-if=" !changeState " >
        <a-date-picker v-model:value="form.updateDate"  placeholder="请输入变更日期"  style="width: 100%"  />
      </a-form-item>
      <a-form-item label="变更信息" name="name" v-if=" !changeState ">
        <DictSelectType  :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="DICT_TYPE_ENUM.TM_PROJECT_UPDATE_REMARK.value"  :height="400" v-model:value="form.updateRemark" />
      </a-form-item>
  </a-form>
    <div class="footer">
      <a-button style="margin-right: 8px" @click="closeModal">取消</a-button>
      <a-button type="primary" @click="handleOk">提交</a-button>
    </div>
  </a-modal>
</template>
<script setup lang="ts">
  import { message } from 'ant-design-vue';
import _ from 'lodash';
import { ref,reactive } from 'vue';
import DictSelectType from '/@/components/cache/dict-select-type.vue'
import { projectApi } from '/@/api/task/project-api';
import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
import { smartSentry } from '/@/lib/smart-sentry';
import { SmartLoading } from '/@/components/framework/smart-loading';

  // ----------------------- 以下是字段定义 emits props ---------------------

  const emit = defineEmits(['refresh']);

  // ----------------------- 显示/隐藏 ------------------------

  const visible = ref(false);
  const projectIdList = ref([]);
  const changeState=ref(false);

  //显示
  async function showModal(selectProjectId,isState) {
    projectIdList.value = selectProjectId;
    changeState.value=isState;
    visible.value = true;
  }

  const formRef = ref();

  const formDefault = {
    projectIdList: [],
    updateValue: undefined,
    changeState: undefined,
    updateDate: undefined,
    updateRemark: undefined,
  };
  let form = reactive({ ...formDefault });


  //隐藏
  function closeModal() {
    visible.value = false;
  }

  // ----------------------- form操作 ---------------------------------
  async function handleOk() {
    SmartLoading.show();
    try {
      if (_.isEmpty(projectIdList.value)) {
        message.warning('请选择要变更的任务');
        return;
      }
      if (_.isEmpty(form.updateValue)) {
        message.warning('请选择要变更的信息');
        return;
      }
      if(!changeState){
        if(!form.updateDate||!form.updateRemark)
          {
            message.warning('变更日志与变更信息不能为空');
            return;
        }
      }
      form.projectIdList=projectIdList.value;
      form.changeState=changeState.value;
      let res=await projectApi.batchUpdateProject(form);
      message.success(res.message);
      emit('refresh');
      closeModal();
    } catch (error) {
      smartSentry.captureError(error);
    } finally {
      SmartLoading.hide();
    }
  }

  // ----------------------- 以下是暴露的方法内容 ----------------------------
  defineExpose({
    showModal,
  });
</script>
<style scoped lang="less">
  .footer {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e9e9e9;
    padding: 10px 16px;
    background: #fff;
    text-align: right;
    z-index: 1;
  }
</style>
